﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BusinessCloud
{
    public class MessageDB
    {
        /// <summary>
        ///  插入黑匣子故障信息
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="m"></param>
        /// <param name="dtzcdm">电梯注册代码</param>
        /// <param name="dtjysbm">电梯救援识别码</param>
        /// <param name="dtqkms">电梯情况描述</param>
        /// <param name="sbsj">上报日期</param>
        /// <param name="zhdzsj">最后动作时间  黑匣子类型是2的时候为恢复供电时间</param>
        /// <param name="hxzlx">黑匣子类型;0、电梯 1 、大屏断电  2 大屏来电  3 心跳包异常 </param>
        /// <param name="zhdzlx">最后动作类型 0 停 1 上 2下</param>
        /// <param name="zhxtbsj">最后心跳时间</param>
        /// <param name="zhdybs">最后电源标识 1 常电 2 备用电</param>
        /// <returns></returns>
        public static DataTable InsertBlackBox(byte[] ids, WebService.MyService m, string dtzcdm, string dtjysbm, string dtqkms, string sbsj, string zhdzsj, string hxzlx, string zhdzlx, string zhxtbsj, string zhdybs)
        {
            if (zhxtbsj == "")
            {
                zhxtbsj = "9998-12-31 00:00:00.000";
            }
            DataSet ds = new DataSet();
            //添加
            SqlHelp sqlhelp = new SqlHelp();
            sqlhelp.DBMCADD(new string[] { "1", "dthxzfa01", "FABM", "insert", "" }, ds);
            string[] insertzdm2 = "id:PK,ts_sx_id,ts_fa_id,dtzcdm,dtqkms,sbrq,zhdzsj,hxzlx,zhdzlx,zhxtbsj,zhdybs".Split(',');
            object[] insertnr2 = { DateTime.Now.Ticks, 9400, 636054670246221546, dtzcdm, dtqkms, sbsj, zhdzsj, hxzlx, zhdzlx, zhxtbsj, zhdybs };//zhxtbsj
            sqlhelp.insertAdd(insertzdm2, insertnr2, ds, "1");

            WebService.WebSlected[] sd = sqlhelp.DataSetToSlecteds(ds);
            sd = m.request_select(sd, ids);
            ds = sqlhelp.SlectedsToDataSet(sd);

            if (sd.Length == 0)
            {
                return new DataTable();
            }
            if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToString().ToLower() != "error")
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }

        }


        /// <summary>
        /// 插入关联帽子与事件
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="m"></param>
        /// <param name="sxid"></param>
        /// <param name="faid"></param>
        /// <param name="eventID">事件id</param>
        /// <param name="mark">帽子askm</param>
        /// <param name="sbrq">上报日期</param>
        /// <param name="lx">设备类型  0  帽子</param>
        /// <param name="ryid">人员id</param>
        /// <returns></returns>
        public static DataTable insertEventAndCap(byte[] ids, WebService.MyService m, string sxid, string faid, string eventID, string mark, string sbrq, string lx, string ryid)
        {
            string id = System.DateTime.Now.Ticks.ToString();
            DataSet ds = new DataSet();
            SqlHelp sqlhelp = new SqlHelp();
            sqlhelp.DBMCADD(new object[] { "1", "hjzxajaqmfa01", "FABM", "insert", "" }, ds);
            string[] selectzdm1 = "id:pk,ts_sx_id,ts_fa_id,eventId,askm,sbrq,lx,tr_ry_id".Split(',');
            string[] selectas1 = new string[] { id, sxid, faid, eventID, mark, sbrq, lx, ryid };
            sqlhelp.insertAdd(selectzdm1, selectas1, ds, "1");
            WebService.WebSlected[] sd = sqlhelp.DataSetToSlecteds(ds);
            sd = m.request_select(sd, ids);
            ds = sqlhelp.SlectedsToDataSet(sd);

            if (sd.Length == 0)
            {
                return new DataTable();
            }
            if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToString().ToLower() != "error")
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }
        /// <summary>
        /// 插入救援帽子轨迹
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="m"></param>
        /// <param name="sxid"></param>
        /// <param name="faid"></param>
        /// <param name="jdzb">经度位置</param>
        /// <param name="wdzb">纬度位置</param>
        /// <param name="gxrq">更新日期</param>
        /// <param name="ryid">人员id</param>
        /// <param name="mark">帽子askm</param>
        /// <returns></returns>
        public static DataTable insertCapTrack(byte[] ids, WebService.MyService m, string sxid, string faid, string jdzb, string wdzb, string gxrq, string ryid, string mark)
        {

            DataSet ds = new DataSet();
            SqlHelp sqlhelp = new SqlHelp();

            sqlhelp.DBMCADD(new object[] { "4", "hjzxajaqm01", "XTBM", "select", "" }, ds);
            string[] selectzdm7 = "id".Split(',');//查询的字段
            string[] selectas7 = "id".Split(',');//字段别名
            sqlhelp.selectAdd("4", selectzdm7, selectas7, ds);
            sqlhelp.whereAdd(new object[] { "4", "", "askm", "等于", mark, "", "并且" }, ds);
            sqlhelp.whereAdd(new object[] { "4", "", "jjcgsj", "等于", "9998-12-31", "", "并且" }, ds);
            WebService.WebSlected[] sd = sqlhelp.DataSetToSlecteds(ds);
            sd = m.request_select(sd, ids);
            ds = sqlhelp.SlectedsToDataSet(sd);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataSet d = new DataSet();
                sqlhelp.DBMCADD(new object[] { "1", "hjzxajaqmfa01", "FABM", "insert", "" }, d);
                string[] selectzdm1 = "id:pk,ts_sx_id,ts_fa_id,jdzb,wdzb,gxrq".Split(',');
                object[] selectas1 = new object[] { ds.Tables[0].Rows[0]["id"].ToString(), sxid, faid, jdzb, wdzb, gxrq };
                sqlhelp.insertAdd(selectzdm1, selectas1, d, "1");

                if (ryid.Length > 0)
                {
                    sqlhelp.DBMCADD(new string[] { "2", "hjzxajaqmfa01", "FABM", "update", "" }, d);
                    string[] updatezdm = "tr_ry_id".Split(',');
                    object[] updatenr = { ryid };
                    sqlhelp.updateAdd(updatezdm, "=", updatenr, d, "2");
                    sqlhelp.whereAdd(new object[] { "2", "", "id", "等于", ds.Tables[0].Rows[0]["id"].ToString(), "", "并且" }, d);
                }

                sd = sqlhelp.DataSetToSlecteds(d);
                sd = m.request_select(sd, ids);
                ds = sqlhelp.SlectedsToDataSet(sd);

            }
            if (sd.Length == 0)
            {
                return new DataTable();
            }
            if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToString().ToLower() != "error")
            {

                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }
        /// <summary>
        /// 领导刷卡插入数据库
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="m"></param>
        /// <param name="dtzcdm">电梯注册代码</param>
        /// <param name="dtjysbm">电梯救援识别码</param>
        /// <param name="nfc">人员卡号</param>
        /// <returns></returns>
        public static DataTable insertLeaderFlushCard(byte[] ids, WebService.MyService m, string dtzcdm, string dtjysbm, string nfc)
        {
            string id = System.DateTime.Now.Ticks.ToString();
            DataSet ds = new DataSet();
            SqlHelp sqlhelp = new SqlHelp();
            sqlhelp.DBMCADD(new object[] { "1", "ldskfa01", "FABM", "insert", "" }, ds);
            string[] selectzdm1 = "id:pk,ts_sx_id,ts_fa_id,dtzcdm,nfc,sbrq".Split(',');
            string[] selectas1 = new string[] { id, "9350", "636092886094397257", dtzcdm, nfc, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
            sqlhelp.insertAdd(selectzdm1, selectas1, ds, "1");
            WebService.WebSlected[] sd = sqlhelp.DataSetToSlecteds(ds);
            sd = m.request_select(sd, ids);
            ds = sqlhelp.SlectedsToDataSet(sd);

            if (sd.Length == 0)
            {
                return new DataTable();
            }
            if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToString().ToLower() != "error")
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }
        /// <summary>
        /// 查找电梯基本信息
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="m"></param>
        /// <param name="bm"></param>
        /// <returns></returns>
        public static DataTable GetElevatorInfo(byte[] ids, WebService.MyService m, string bm)
        {
            DataSet ds = new DataSet();
            SqlHelp sqlhelp = new SqlHelp();
            WebService.WebSlected[] sd = sqlhelp.DataSetToSlecteds(ds);
            sqlhelp.DBMCADD(new object[] { "1", "DTJBXX01", "XTBM", "select", "" }, ds);//主表1，及主表1的查询字段
            string[] selectzdm7 = "dtid,dtzcdm,dtjysbm,dtmc,jdwd,ts_qx_id,qxmc,dtwz,cjid,cjmc,sydwid,sydwmc,wbdwid,wbdwmc,zgjgid,zgjgmc,jydwid,jydwmc,xcjyrq,bkptip,bkptport".Split(',');
            string[] selectas7 = new string[] { "id", "dtzcdm", "dtjysbm", "dtmc", "jdwd", "ts_qx_id", "qxmc", "dtwz", "cjid", "cjmc", "sydwid", "sydwmc", "wbdwid", "wbdwmc", "zgjgid", "zgjgmc", "jydwid", "jydwmc", "xcjyrq","bkptip" ,"bkptport"};
            sqlhelp.selectAdd("1", selectzdm7, selectas7, ds);
            sqlhelp.whereAdd(new object[] { "1", "", "dtjysbm", "等于", bm, "", "或者" }, ds);
            sqlhelp.whereAdd(new object[] { "1", "", "dtzcdm", "等于", bm, "", "并且" }, ds);


            sd = sqlhelp.DataSetToSlecteds(ds);
            sd = m.request_select(sd, ids);
            ds = sqlhelp.SlectedsToDataSet(sd);
            if (sd.Length == 0)
            {
                return new DataTable();
            }
            if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToString().ToLower() != "error")
            {
                ds.Tables[0].Columns.Add("longitude", typeof(double));
                ds.Tables[0].Columns.Add("latitude", typeof(double));
                //将查询出电梯的经度纬度从坐标类中取出
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["longitude"] = 0;
                    ds.Tables[0].Rows[i]["latitude"] = 0;
                    if (ds.Tables[0].Rows[i]["jdwd"].GetType() == typeof(WebService.WebzbClass[]) && ((WebService.WebzbClass[])ds.Tables[0].Rows[i]["jdwd"]).Length > 0)
                    {
                        try
                        {
                            WebService.WebzbClass zb = ((WebService.WebzbClass[])ds.Tables[0].Rows[i]["jdwd"])[0];
                            ds.Tables[0].Rows[i]["longitude"] = zb.jd;
                            ds.Tables[0].Rows[i]["latitude"] = zb.wd;
                        }
                        catch
                        {
                        }
                    }
                }
                ds.Tables[0].Columns.Remove("jdwd");
                ds.Tables[0].TableName = "电梯基本信息表";
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }
        /// <summary>
        /// 获取所有上级单位
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="m"></param>
        /// <param name="qxid"></param>
        /// <returns></returns>
        public static DataTable GetAllUp(byte[] ids, WebService.MyService m, string qxid)
        {
            DataSet ds = new DataSet();
            SqlHelp sqlhelp = new SqlHelp();
            
            sqlhelp.DBMCADD(new object[] { "4", "bigscreen01", "XTBM", "select", "" }, ds);
            string[] selectzdm1 = new string[] { "dbo.fn_sjqx(" + qxid + ",4)" };//fn_qx(辖区id,层数) 查询 辖区id的所有下级辖区
            string[] selectas1 = "*".Split(',');
            sqlhelp.selectAdd("4", selectzdm1, selectas1, ds);
            WebService.WebSlected[] sd = sqlhelp.DataSetToSlecteds(ds);
            sd = m.request_select(sd, ids);
            ds = sqlhelp.SlectedsToDataSet(sd);

            if (sd.Length == 0)
            {
                return new DataTable();
            }
            if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToString().ToLower() != "error")
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }
    }
}
